# Copyright 2019 The Kubernetes Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# Enable Go modules.
export GO111MODULE=on

REGISTRY ?= gcr.io/k8s-staging-gateway-api
TAG ?= dev
COMMIT=$(shell git rev-parse --short HEAD)

DOCKER ?= docker
# TOP is the current directory where this Makefile lives.
TOP := $(dir $(firstword $(MAKEFILE_LIST)))
# ROOT is the root of the mkdocs tree.
ROOT := $(abspath $(TOP))

all: generate vet fmt verify test

# Run generators for protos, Deepcopy funcs, CRDs, and docs.
.PHONY: generate
generate:
	hack/update-codegen.sh

# Run go fmt against code
fmt:
	go fmt ./...

# Run go vet against code
vet:
	go vet ./...

# Run go test against code
test:
	go test ./...

# Install CRD's and example resources to a pre-existing cluster.
.PHONY: install
install: crd example

# Install the CRD's to a pre-existing cluster.
.PHONY: crd
crd:
	kubectl kustomize config/crd | kubectl apply -f -

# Install the example resources to a pre-existing cluster.
.PHONY: example
example:
	hack/install-examples.sh

# Remove installed CRD's and CR's.
.PHONY: uninstall
uninstall:
	hack/delete-crds.sh

# Run static analysis.
.PHONY: verify
verify:
	hack/verify-all.sh

# Build the documentation.
.PHONY: docs
docs:
	# Ensure site dir exists
	mkdir -p site
	# Generate docs with mkdocs
	mkdocs build
	# Generate v1alpha1 API docs
	./hack/api-docs/generate.sh v1alpha1 site/v1alpha1-spec.html
	# Add them to spec page originally generated by mkdocs
	sed -i -e '/REPLACE_WITH_GENERATED_CONTENT/{r site/v1alpha1-spec.html' -e 'd}' site/references/spec/index.html
	# Generate v1alpha2 API docs
	./hack/api-docs/generate.sh v1alpha2 site/v1alpha2-spec.html
	# Add them to spec page originally generated by mkdocs
	sed -i -e '/REPLACE_WITH_GENERATED_CONTENT/{r site/v1alpha2-spec.html' -e 'd}' site/v1alpha2/references/spec/index.html

.PHONY: build
build:
	docker build --build-arg COMMIT=$(COMMIT) --build-arg TAG=$(TAG) \
			-t $(REGISTRY)/admission-server:$(TAG) .

.PHONY: release-staging
release-staging: build
	docker push $(REGISTRY)/admission-server:$(TAG)
	docker tag $(REGISTRY)/admission-server:$(TAG) $(REGISTRY)/admission-server:latest
	docker push $(REGISTRY)/admission-server:latest
